package com.hllxd.hism.star.basic.service.service;

import com.hllxd.hism.star.basic.service.dao.mapper.mis.TblDataResourceBudgetMngMapper;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.TblDataResourceGroupMngMapper;
import com.hllxd.hism.star.basic.service.model.TblDataResourceBudgetMng;
import com.hllxd.hism.star.basic.service.model.TblDataResourceBudgetMngExample;
import com.hllxd.hism.star.basic.service.model.TblDataResourceGroupMng;
import com.hllxd.hism.star.basic.service.model.TblDataResourceGroupMngExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service("ResourceBudgetService")
public class ResourceBudgetServiceImpl implements ResourceBudgetService {

    @Autowired
    private TblDataResourceBudgetMngMapper resourceBudgetMngMapper;

    @Autowired
    private TblDataResourceGroupMngMapper resourceGroupMngMapper;

    @Override
    public Integer updateValid(TblDataResourceBudgetMng entity) {
        TblDataResourceBudgetMng aa = new TblDataResourceBudgetMng();
        aa.setId(entity.getId());
        aa.setApplyStatus(entity.getApplyStatus());
        return resourceBudgetMngMapper.updateByPrimaryKeySelective(aa);
    }

    @Override
    public TblDataResourceBudgetMng getResourceBudget(Integer id) {
        return resourceBudgetMngMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer delResourceBudget(Integer id, String userName) {
        TblDataResourceBudgetMng aa = new TblDataResourceBudgetMng();
        aa.setId(id);
        aa.setAction((byte) 2);
        return resourceBudgetMngMapper.updateByPrimaryKeySelective(aa);
    }

    @Override
    public Integer setResourceBudget(TblDataResourceBudgetMng aa) {
        //工单token:21b3e3ab47a7457581f5160bfa4ace63
        //正确返回：{"code": 0, "data": {"status": 1, "msg": "ok", "data": ["107909690562626"]}}
        //错误返回：{"code": 0, "data": {"status": 0, "msg": "生成工单时出错，错误原因为：自动生成工单失败，已有相同记录工单", "data": []}}
        int id;
        if (aa.getId() != null && aa.getId() > 0) {
            id = resourceBudgetMngMapper.updateByPrimaryKeySelective(aa);
        } else {
            aa.setApplyStatus(0);
            id = resourceBudgetMngMapper.insertSelective(aa);
        }
        return id;
    }

    @Override
    public List<TblDataResourceBudgetMng> listResourceBudget() {
        TblDataResourceBudgetMngExample resourceBudgetMngExample = new TblDataResourceBudgetMngExample();
        TblDataResourceBudgetMngExample.Criteria criteria = resourceBudgetMngExample.createCriteria();
        criteria.andActionIn(Arrays.asList(new Byte[]{0, 1}));
        return resourceBudgetMngMapper.selectByExample(resourceBudgetMngExample);
    }

    @Override
    public TblDataResourceGroupMng getResourceGroup(Integer id) {
        return resourceGroupMngMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer delResourceGroup(Integer id, String userName) {
        TblDataResourceGroupMng aa = new TblDataResourceGroupMng();
        aa.setId(id);
        aa.setAction((byte) 2);
        return resourceGroupMngMapper.updateByPrimaryKeySelective(aa);
    }

    @Override
    public Integer setResourceGroup(TblDataResourceGroupMng aa) {
        int id;
        if (aa.getId() != null && aa.getId() > 0) {
            id = resourceGroupMngMapper.updateByPrimaryKeySelective(aa);
        } else {
            id = resourceGroupMngMapper.insertSelective(aa);
        }
        return id;
    }

    @Override
    public List<TblDataResourceGroupMng> listResourceGroup() {
        TblDataResourceGroupMngExample resourceGroupMngExample = new TblDataResourceGroupMngExample();
        TblDataResourceGroupMngExample.Criteria criteria = resourceGroupMngExample.createCriteria();
        criteria.andActionIn(Arrays.asList(new Byte[]{0, 1}));
        return resourceGroupMngMapper.selectByExample(resourceGroupMngExample);
    }

    @Override
    public Integer updateBudgetStatus(String order_id, Integer applyStatus) {
        TblDataResourceBudgetMng aa = new TblDataResourceBudgetMng();
        aa.setOrderId(order_id);
        aa.setApplyStatus(applyStatus);
        return resourceBudgetMngMapper.updateByPrimaryKeySelective(aa);
    }
}
